package rwt.server.handlers.web;

import rwt.server.*;
import rwt.server.RailroadMessages.LocoDirfMsg;
import rwt.server.WebMessages.SetTrainBell;
import rwt.server.core.*;
import rwt.server.railroad.*;

public class SetTrainBellHandler extends MessageHandler<SetTrainBell>
{
	@Override
	public void handle(SetTrainBell message, IConnection source) throws Exception
	{
		Train t = RailServer.getTrainManager().getTrainById(message.trainId);
		int slot = t.getVirtualSlotNum();
		LocoDirfMsg railMsg = new LocoDirfMsg(slot, 
				t.getDirection(), 
				t.getLight(), 
				t.getHorn(), 
				message.bell);
		
		RailroadSystem system = t.isEmergency() ? RailServer.getRailroadServer() : RailServer.getController();
		system.write(railMsg);
	}
}
